package com.weipai.education.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * @Author: 曹朋飞
 * @Date: 2019/01/19 17:37
 */
public class GenerateUserAccountNumberUtils {

    /** 用户id和随机数总长度 */
    private static final int maxLength = 8;

    /** 随即编码 */
    private static final int[] code = new int[]{7, 9, 6, 2, 8 , 1, 3, 0, 5, 4};

    /**
     * 生成经销商账号
     *
     * @return
     */
    public static String generaateUserAccountNumber() {
        return getDateTime();
    }

    /**
     * 更具id进行加密+加随机数组成固定长度编码
     */
    private static String toCode(Long id) {
        String idStr = id.toString();
        StringBuilder idsbs = new StringBuilder();
        for (int i = idStr.length() - 1 ; i >= 0; i--) {
            idsbs.append(code[idStr.charAt(i)-'0']);
        }
        return idsbs.append(getRandom(maxLength - idStr.length())).toString();
    }

    /**
     * 生成时间戳
     */
    private static String getDateTime(){
       // DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
       /* Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSS");
        String  formDate =sdf.format(date);
        String no = formDate.substring(10);*/
        //return sdf.format(new Date());
       // UUID uuid = UUID.randomUUID();



        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String str1 = simpleDateFormat.format(new Date());
        String str2 = Integer.toString((int)((Math.random()*9+1)*100000));
        return str1 + str2;



       // return uuid.toString();
    }

    /**
     * 生成固定长度随机码
     * @param len 长度
     */
    private static long getRandom(long len) {
        long min = 1,max = 9;
        for (int i = 1; i < len; i++) {
            min *= 10;
            max *= 10;
        }
        long rangeLong = (((long) (new Random().nextDouble() * (max - min)))) + min ;
        return rangeLong;
    }

    /**
     * 生成不带类别标头的编码
     */
    private static synchronized String getCode(){
        //id = id == null ? 1000 : id;
        int count = 0;
        String result = "";
        try {
            Random random = new Random();
            for(int i = 0; i < 100; i++)
            {
                count = random.nextInt(51) + 10;
            }
            result = getDateTime() + toCode(Long.valueOf(count));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    
}
